package com.deyuanyun.pic.pbf.service.impl;

import com.deyuanyun.pic.common.util.MapUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.insp.service.inspresult.InspCommService;
import com.deyuanyun.pic.insp.util.InspTaskVO;
import com.deyuanyun.pic.pbf.mapper.FlawStatisticsMapper;
import com.deyuanyun.pic.pbf.service.FlawStatisticsService;
import com.deyuanyun.pic.settings.service.base.BizMsgThresholdService;
import com.deyuanyun.pic.settings.support.biz.CodeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 仪表板缺陷统计 实现
 *
 * @author scalpel.王敏
 * @version [v1.0] 创建时间（2016/6/15）
 * @see // 引用的类
 * Method List:
 * 1.----------------
 * 2.----------------
 * History:
 */
@Service
public class FlawStatisticsServiceImpl  implements FlawStatisticsService {


    @Resource
    private InspCommService inspCommService;
    @Resource
    private FlawStatisticsMapper flawStatisticsMapper;

    @Override
    public Map<String, Object> queryInnerInspection(Map<String, String> param) {
        Map<String,Object> result = new HashMap<String,Object>();
        String lineId = param.get("lineId");
        String beginMileage = param.get("beginMileage");
        String endMileage = param.get("endMileage");
        List<InspTaskVO>  data = inspCommService.queryNewTaskMileage(lineId,StringUtils.isEmpty(beginMileage)?null:Double.parseDouble(beginMileage),StringUtils.isEmpty(endMileage)?null:Double.parseDouble(endMileage));

        List<Map<String, Object>> inspData = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> t;
        Map tm = new HashMap();
        tm.putAll(param);
        for(InspTaskVO itv :data)
        {

            tm.put("taskId",itv.getId());
            tm.put("beginMileage",itv.getFromMileage().toString());
            tm.put("endMileage",itv.getToMileage().toString());
            t= flawStatisticsMapper.queryInnerInspection(tm);
            if (inspData.size() == 0)
            {
                inspData.addAll(t);
            }else{
                for(int i=0,e=inspData.size();i<e;i++)
                {
                    MapUtil.mergeAdd(inspData.get(i),t.get(i));
                }
            }
        }

        if(data.size() > 0)
        {
            result.put("inspData",inspData);
            result.put("pagDate",data.get(0).getPagDateTime() == null?"":StringUtils.convertDateToString(data.get(0).getPagDateTime(),"yyyy年MM月dd日"));
        }
        return   result;
    }

    @Override
    public  List<Map<String, Object>> queryOtherInspection(Map<String, String> param) {

        Map<String, Object> dateMap = flawStatisticsMapper.queryOtherNewDate(param);


        Date foundTime =null;
        if(dateMap != null)
        {
            foundTime = (Date) dateMap.get("FOUND_TIME");

        }
        Calendar calendar= Calendar.getInstance();
        do {
             param.put("mendTime",StringUtils.convertDateToString(calendar.getTime(),"yyyy-MM-dd")+" 23:59:59");
             param.put("noInsp_end",StringUtils.convertDateToString(calendar.getTime(),"yyyy年MM月dd日"));

            calendar.add(Calendar.YEAR,-1);
            calendar.add(Calendar.MONTH,1);
            param.put("mbeginTime", StringUtils.convertDateToString(calendar.getTime(),"yyyy-MM-01"));
            param.put("noInsp_begin",StringUtils.convertDateToString(calendar.getTime(),"yyyy年MM月01日"));
            calendar.add(Calendar.DATE,-calendar.getTime().getDate());

        }while (dateMap!=null&& calendar.getTime().getTime() >= foundTime.getTime() );
        //最近12个月  包含本月有数据，如果木有，再向后推12个月





        List<Map<String, Object>> list=flawStatisticsMapper.queryOtherInspection(param);
        for(Map<String, Object> m:list)
        {
            m.put("TYPE", CodeUtil.getName((String)m.get("TYPE")));
            if(m.get("TYPE") == null)
            {
                m.put("TYPE","未知");
            }
        }
        return list;
    }

    @Override
    public  List<Map<String, Object>> queryOtherInspectionBar(Map<String, Object> param) {
        //设置查询时间段




        List<Map<String, Object>> list=flawStatisticsMapper.queryOtherInspectionBar(param);
        for(Map<String, Object> m:list)
        {
            m.put("TYPE", CodeUtil.getName((String)m.get("TYPE")));
            if(m.get("TYPE") == null)
            {
                m.put("TYPE","未知");
            }
        }
        return list;
    }
}
